All files / src/app/api/admin/monitoring/performance/endpoints/[path] route.ts

0% Statements 0/71
100% Branches 0/0
0% Functions 0/1
0% Lines 0/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                                                                                                                               
/**
 * Endpoint Details API
 *
 * GET /api/admin/monitoring/performance/endpoints/[path]
 *
 * Returns detailed performance data for a specific endpoint.
 */

export const dynamic = 'force-dynamic';

import { NextRequest, NextResponse } from 'next/server';
import { getEndpointDetails } from '@/lib/monitoring/performanceQueries';
import { logger } from '@/lib/logging';
import {
  withAdmin,
  withErrorHandling,
  successResponse,
  notFoundResponse,
  errorResponse,
  type ApiSuccessResponse,
  type ApiErrorResponse,
  type RouteContext,
} from '@/lib/api';
import type { EndpointDetails } from '@/lib/monitoring/performanceQueries';

/**
 * GET handler for endpoint details
 */
async function handleGet(
  request: NextRequest,
  context?: RouteContext
): Promise<NextResponse<ApiSuccessResponse<EndpointDetails> | ApiErrorResponse>> {
  // Extract path from context params
  const params = context?.params as { path?: string } | undefined;
  const path = params?.path;

  if (!path) {
    return errorResponse('INVALID_PARAM', 'Path parameter is required', { status: 400 });
  }

  // Parse query parameters
  const { searchParams } = new URL(request.url);
  const timeRange = searchParams.get('timeRange') || '24h';

  // Validate time range
  const validRanges = ['1h', '6h', '24h', '7d', '30d'];
  if (!validRanges.includes(timeRange)) {
    return errorResponse(
      'INVALID_PARAM',
      'Invalid timeRange. Must be one of: 1h, 6h, 24h, 7d, 30d',
      { status: 400 }
    );
  }

  try {
    const details = await getEndpointDetails(path, timeRange);

    if (!details) {
      return notFoundResponse('Endpoint not found or has no metrics');
    }

    return successResponse(details);
  } catch (error) {
    logger.error('Error fetching endpoint details', error instanceof Error ? error : new Error(String(error)), { category: 'API' });
    return errorResponse('INTERNAL_ERROR', 'Failed to fetch endpoint details', {
      status: 500,
    });
  }
}

export const GET = withErrorHandling(withAdmin(handleGet));